This unique method, simply takes an array and it returns that array with all
duplicatesremoved.Thatmeans ifIhavethe samenumberafew timesorthe
samestring,it'llremoveanyduplicates.Let'srunthis.
BackinsideAtom,wecanaddthisutilityintoourproject,we'llcommentoutour
_.isStringcallsandwewillmakeavariablecalledfilteredArray.Thiswillbethe
arraywithouttheduplicates,andwhatwe'lldoiscall,aftertheequalsign,_.uniq.
Now,asweknow,thistakesanarray.Andsincewe'retryingtousetheunique
function,we'llpassinanarraywithsomeduplicates.Useyournametwiceasa
string;I'llusemynameonce,followedbythenumber1,followedbymyname
again.ThenIcanuse1,2,3,and4asshownhere:
console.log('Startingapp.js');
constfs=require('fs');
constos=require('os');
const_=require('lodash');
constnotes=require('./notes.js');
//console.log(_.isString(true));
//console.log(_.isString('Gary'));
varfilteredArray=_.uniq(['Gary',1,'Gary',1,2,3,4]);
console.log();
//console.log('Result:',notes.add(9,-2));
//varuser=os.userInfo();
//
//fs.appendFile('greetings.txt',`Hello${user.username}!Youare${notes.age}.`);
Now, if things go as planned, we should get an array with all the duplicates
removed,which means we'll have one instance of Gary, one instance of 1, and
then2,3,and4,whichdon'thaveduplicates.
Thelastthingtodoistoprintthatusingconsole.logsowecanviewitinsidethe
Terminal. I'll pass in this filteredArray variable to our console.log statement as
showninthefollowingcode:
console.log('Startingapp.js');
constfs=require('fs');
constos=require('os');
const_=require('lodash');
constnotes=require('./notes.js');
//console.log(_.isString(true));
//console.log(_.isString('Gary'));
varfilteredArray=_.uniq(['Gary',1,'Gary',1,2,3,4]);
console.log(filteredArray);